<script setup lang="ts">
import { getCartDetailAPI, PostCartDetailAPI } from '@/services/cart'
import type { CartDataItem } from '@/types/cart'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import { postMemberOrderAPI } from '@/services/order'
import { url } from 'inspector'

//获取购物车列表
const CartList = ref<CartDataItem[]>([])
const getCartDataList = async () => {
  const res = await getCartDetailAPI({
    page: '1',
    size: '10',
  })
  // console.log(res)
  CartList.value = res.data
}

//购物车数目减少
const jianCount = async (data: { meal_id: any; meal_name: any; meal_spec: any; count: number }) => {
  if (data.count > 1) {
    const res = await PostCartDetailAPI({
      meal_id: data.meal_id,
      meal_name: data.meal_name,
      meal_spec: data.meal_spec,
      count: data.count - 1,
    })
  }

  getCartDataList()
}

//购物车数目添加
const addCount = async (data: { meal_id: any; meal_name: any; meal_spec: any; count: number }) => {
  const res = await PostCartDetailAPI({
    meal_id: data.meal_id,
    meal_name: data.meal_name,
    meal_spec: data.meal_spec,
    count: data.count + 1,
  })
  getCartDataList()
}

//单选按钮控制
const selectGoodslist = ref<CartDataItem[]>([])
//单选选中
const selectOneChert = ref(false)
const onChangeSelected = async (data: any) => {
  let flack = false
  let ind = 0
  selectGoodslist.value.some((item, index) => {
    if (item.id == data.id) {
      flack = true
      ind = index
      return
    }
  })
  if (flack) {
    selectGoodslist.value.splice(ind, 1)
  } else {
    selectGoodslist.value.push(data)
  }

  if (selectGoodslist.value.length == CartList.value.length) {
    selectAllChert.value = true
    selectOneChert.value = true
  } else {
    selectAllChert.value = false
  }
  // console.log(selectGoodslist.value)
}

const inputCount = async (data: any) => {}

//全选按钮控制
const selectAllChert = ref(false)

//全选选中
const selectAll = () => {
  if (!selectAllChert.value) {
    selectAllChert.value = true
    selectGoodslist.value = []
    CartList.value.forEach((item) => {
      selectGoodslist.value.push(item)
    })
    selectOneChert.value = true
  } else {
    selectGoodslist.value = []
    selectAllChert.value = false
    selectOneChert.value = false
  }
  // selectOneChert.value = selectAllChert.value
  // console.log(selectGoodslist.value)

  return selectAllChert.value
}

// 计算总金额
const hadleprice = computed(() => {
  let pricea = 0
  selectGoodslist.value.forEach((item) => {
    pricea = pricea + item.price * item.count
  })
  return pricea
})

//是否结算
const handlepayment = computed(() => {
  if (selectGoodslist.value.length > 0) {
    return false
  } else {
    return true
  }
})

const hadleOrder = async () => {
  if (selectGoodslist.value.length > 0) {
    let orderlist: string[] = []
    selectGoodslist.value.forEach((item) => {
      orderlist.push(item.id.toString())
    })
    try {
      const res = await postMemberOrderAPI({
        cartList: orderlist,
        shop_id: '5',
      })
      console.log(res)
      uni.showToast({
        title: '下单成功',
        icon: 'none',
      })

      setTimeout(() => {
        //创建订单成功后默认跳转到待发货页面
        // uni.switchTab({ url: '/pages/my/my' })
        uni.navigateTo({ url: '/pagesOrder/list/list?type=1' })
      }, 500)
    } catch (error) {
      uni.showToast({
        title: '订单新建失败',
        icon: 'none',
        duration: 2000,
      })
    }
  } else {
    uni.showToast({
      title: '请选择商品',
      icon: 'none',
      duration: 2000,
    })
  }
}

onLoad(() => {
  getCartDataList()
})

onShow(() => {
  getCartDataList()
})
</script>

<template>
  <scroll-view scroll-y class="scroll-view">
    <!-- 已登录: 显示购物车 -->
    <template v-if="true">
      <!-- 购物车列表 -->
      <view class="cart-list" v-if="true">
        <!-- 优惠提示 -->
        <!-- <view class="tips">
          <text class="label">满减</text>
          <text class="desc">满1件, 即可享受9折优惠</text>
        </view> -->
        <!-- 滑动操作分区 -->
        <uni-swipe-action>
          <!-- 滑动操作项 -->
          <uni-swipe-action-item v-for="item in CartList" :key="item.id" class="cart-swipe">
            <!-- 商品信息 -->
            <view class="goods">
              <!-- 选中状态 -->
              <!-- <text
                @tap="onChangeSelected(item)"
                class="checkbox"
                :class="{ checked: onChangeSelected }"
              ></text> -->
              <checkbox @tap="onChangeSelected(item)" class="checkbox" :checked="selectOneChert" />
              <navigator
                :url="`/pages/goods/goods?id=${item.meal_id}`"
                hover-class="none"
                class="navigator"
              >
                <image mode="aspectFill" class="picture" :src="item.pictureList[0].url"></image>
                <view class="meta">
                  <view class="name ellipsis">{{ item.meal_name }}</view>
                  <view class="attrsText ellipsis">{{ item.mealdesc }}</view>
                  <view class="price">{{ item.price || 69.0 }}</view>
                </view>
              </navigator>
              <!-- 商品数量 -->
              <view class="count">
                <text class="text" @tap="jianCount(item)">- </text>
                <input
                  class="input"
                  @tap="inputCount(item)"
                  type="number"
                  :value="String(item.count)"
                />
                <text class="text" @tap="addCount(item)"> +</text>
              </view>
            </view>
            <!-- 右侧删除按钮 -->
            <template #right>
              <view class="cart-swipe-right">
                <button class="button delete-button">删除</button>
              </view>
            </template>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>
      <!-- 购物车空状态 -->
      <view class="cart-blank" v-else>
        <image src="/static/images/blank_cart.png" class="image" />
        <text class="text">购物车还是空的，快来挑选好货吧</text>
        <navigator open-type="switchTab" url="/pages/index/index" hover-class="none">
          <button class="button">去首页看看</button>
        </navigator>
      </view>
      <!-- 吸底工具栏 -->
      <view class="toolbar">
        <!-- <text class="all" @tap="selectAll" :class="{ checked: selectAllChert }">全选</text> -->
        <checkbox class="all" @tap="selectAll" :checked="selectAllChert" />
        <text class="text">合计:</text>
        <text class="amount">{{ hadleprice }}</text>
        <view class="button-grounp">
          <view
            class="button payment-button"
            :class="{ disabled: handlepayment }"
            @tap="hadleOrder"
          >
            去结算
          </view>
        </view>
      </view>
    </template>
    <!-- 未登录: 提示登录 -->
    <view class="login-blank" v-else>
      <text class="text">登录后可查看购物车中的商品</text>
      <navigator url="/pages/login/login" hover-class="none">
        <button class="button">去登录</button>
      </navigator>
    </view>
    <!-- 猜你喜欢 -->
    <!--  <XtxGuess ref="guessRef"></XtxGuess> -->
    <!-- 底部占位空盒子 -->
    <view class="toolbar-height"></view>
  </scroll-view>
</template>

<style lang="scss">
// 根元素
:host {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f7f7f8;
}

// 滚动容器
.scroll-view {
  flex: 1;
}

// 购物车列表
.cart-list {
  padding: 0 20rpx;

  // 优惠提示
  .tips {
    display: flex;
    align-items: center;
    line-height: 1;
    margin: 30rpx 10rpx;
    font-size: 26rpx;
    color: #666;

    .label {
      color: #fff;
      padding: 7rpx 15rpx 5rpx;
      border-radius: 4rpx;
      font-size: 24rpx;
      background-color: #27ba9b;
      margin-right: 10rpx;
    }
  }

  // 购物车商品
  .goods {
    display: flex;
    padding: 20rpx 20rpx 20rpx 80rpx;
    border-radius: 10rpx;
    background-color: #fff;
    position: relative;

    .navigator {
      display: flex;
    }

    .checkbox {
      position: absolute;
      top: 0;
      left: 0;

      display: flex;
      align-items: center;
      justify-content: center;
      width: 80rpx;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
      // &::before {
      //   content: url(../../../static//images/buxuanzhong.png);
      //   font-family: 'erabbit' !important;
      //   font-size: 40rpx;
      //   color: #444;
      // }

      // &.checked::before {
      //   content: url(../../../static//images/xuanzhong.png);
      //   color: #27ba9b;
      // }
    }

    .picture {
      width: 170rpx;
      height: 170rpx;
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 20rpx;
    }

    .name {
      height: 72rpx;
      font-size: 26rpx;
      color: #444;
    }

    .attrsText {
      line-height: 1.8;
      padding: 0 15rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .price {
      line-height: 1;
      font-size: 26rpx;
      color: #444;
      margin-bottom: 2rpx;
      color: #cf4444;

      &::before {
        content: '￥';
        font-size: 80%;
      }
    }

    // 商品数量
    .count {
      position: absolute;
      bottom: 20rpx;
      right: 5rpx;

      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 220rpx;
      height: 48rpx;

      .text {
        height: 100%;
        padding: 0 20rpx;
        font-size: 36rpx;
        color: #ffffff;
        background-color: #27ba9b;
      }

      .input {
        height: 100%;
        text-align: center;
        border-radius: 4rpx;
        font-size: 24rpx;
        color: #444;
        background-color: #f6f6f6;
      }
    }
  }

  .cart-swipe {
    display: block;
    margin: 20rpx 0;
  }

  .cart-swipe-right {
    display: flex;
    height: 100%;

    .button {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 50px;
      padding: 6px;
      line-height: 1.5;
      color: #fff;
      font-size: 26rpx;
      border-radius: 0;
    }

    .delete-button {
      background-color: #cf4444;
    }
  }
}

// 空状态
.cart-blank,
.login-blank {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 60vh;
  .image {
    width: 400rpx;
    height: 281rpx;
  }
  .text {
    color: #444;
    font-size: 26rpx;
    margin: 20rpx 0;
  }
  .button {
    width: 240rpx !important;
    height: 60rpx;
    line-height: 60rpx;
    margin-top: 20rpx;
    font-size: 26rpx;
    border-radius: 60rpx;
    color: #fff;
    background-color: #27ba9b;
  }
}

// 吸底工具栏
.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--window-bottom);
  z-index: 1;

  height: 100rpx;
  padding: 0 20rpx;
  display: flex;
  align-items: center;
  border-top: 1rpx solid #ededed;
  border-bottom: 1rpx solid #ededed;
  background-color: #fff;
  box-sizing: content-box;

  .all {
    margin-left: 25rpx;
    font-size: 14px;
    color: #444;
    display: flex;
    align-items: center;
  }

  // .all::before {
  //   font-family: 'erabbit' !important;
  //   content: url(../../../static//images/buxuanzhong.png);
  //   font-size: 40rpx;
  //   margin-right: 8rpx;
  // }

  // .checked::before {
  //   content: url(../../../static//images/xuanzhong.png);
  //   color: #27ba9b;
  // }

  // .text {
  //   margin-right: 8rpx;
  //   margin-left: 32rpx;
  //   color: #444;
  //   font-size: 14px;
  // }

  .textcount {
    margin-right: 8rpx;
    margin-left: 32rpx;
    color: #444;
    font-size: 14px;
  }

  .amount {
    font-size: 20px;
    color: #cf4444;

    .decimal {
      font-size: 12px;
    }

    &::before {
      content: '￥';
      font-size: 12px;
    }
  }

  .button-grounp {
    margin-left: auto;
    display: flex;
    justify-content: space-between;
    text-align: center;
    line-height: 72rpx;
    font-size: 13px;
    color: #fff;

    .button {
      width: 240rpx;
      margin: 0 10rpx;
      border-radius: 72rpx;
    }

    .payment-button {
      background-color: #27ba9b;

      &.disabled {
        opacity: 0.6;
      }
    }
  }
}
// 底部占位空盒子
.toolbar-height {
  height: 100rpx;
}
</style>
